package bluetooth.main;

import java.io.IOException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;

public class frmMain extends Shell {
	private Button btnServer;
	private StartServer server = new StartServer();
	private List listMessage;

	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			frmMain shell = new frmMain(display);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * @param display
	 */
	int i=0;
	private Button btnShowRecivedData;
	public frmMain(Display display) {
		super(display, SWT.SHELL_TRIM);
		setLayout(new FormLayout());
		
		btnServer = new Button(this, SWT.NONE);
		btnServer.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				  btnServer.setEnabled(false);
					 Runnable r = new Runnable() {
				         public void run() {
				        	 try {
									
									server.startBluetoothServer(listMessage,btnServer);
								} catch (IOException e) {
									
									e.printStackTrace();
								}
				         }
				     };

				     new Thread(r).start();
			}
		});
		
		FormData fd_btnServer = new FormData();
		fd_btnServer.left = new FormAttachment(0, 10);
		fd_btnServer.top = new FormAttachment(0);
		fd_btnServer.right = new FormAttachment(0, 126);
		btnServer.setLayoutData(fd_btnServer);
		btnServer.setImage(SWTResourceManager.getImage(frmMain.class, "/images/bluetooth_off.png"));
		
		listMessage = new List(this, SWT.BORDER|SWT.V_SCROLL);
		fd_btnServer.bottom = new FormAttachment(listMessage, -6);
		
		FormData fd_listMessage = new FormData();
		fd_listMessage.right = new FormAttachment(100, -10);
		fd_listMessage.left = new FormAttachment(0, 10);
		fd_listMessage.bottom = new FormAttachment(100, -10);
		fd_listMessage.top = new FormAttachment(0, 70);
		
		listMessage.setLayoutData(fd_listMessage);
		
		btnShowRecivedData = new Button(this, SWT.NONE);
		
		btnShowRecivedData.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				
				//Open form for display reviced data from Android 
				frmShowRecivedData frmShow = new frmShowRecivedData(getDisplay());
				frmShow.open();
			}
		});
		FormData fd_btnShowRecivedData = new FormData();
		fd_btnShowRecivedData.bottom = new FormAttachment(btnServer, 0, SWT.BOTTOM);
		fd_btnShowRecivedData.left = new FormAttachment(btnServer, 6);
		btnShowRecivedData.setLayoutData(fd_btnShowRecivedData);
		btnShowRecivedData.setText("Show recived data from android");
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("Bluetooth server");
		setSize(450, 300);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
